package servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import pojo.Order;
import pojo.User;

/**
 * Servlet implementation class ShoppingCartServlet
 */
public class ShoppingCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
    public ShoppingCartServlet() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Map<String,Order> orderMap = (Map<String, Order>) request.getSession().getAttribute("orderMap");
		User user = (User) request.getSession().getAttribute("user");
		String bid = request.getParameter("bid");
		String bname = request.getParameter("bname");
		
		
		
		if(orderMap==null){
			orderMap = new HashMap<String,Order>();
			request.getSession().setAttribute("orderMap", orderMap);
		}
		Logger logger = Logger.getLogger(ShoppingCartServlet.class);
		if(bid!=null){
			Order order = orderMap.get(bid);
			if(order==null){
				order = new Order(null,user.getUid(),bid,1);
				order.setBname(bname);
				orderMap.put(bid, order);
			}else{
				order.setCount(order.getCount()+1);
			}

			logger.info("添加了订单： "+ bid+", "+ bname);
		}
		
		///跳转到“我的购物车”界面
		logger.info("跳转到“我的购物车”界面");
		
		request.getRequestDispatcher("../stoppingcart.jsp").forward(request, response);
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		this.doGet(request, response);
	}

}
